package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import model.Book;
import model.User;
import util.StringUtil;

/**
 * 用户Dao类
 * @author 梁
 *
 */
public class UserDao {

	/**
	 * 登录验证
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public User login(Connection con,User user)throws Exception {
		User resultUser=null;
		String sql="select * from t_user where userName=? and password=? and usertype=?";
		PreparedStatement pstmt= con.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getPassWord());
		pstmt.setString(3, user.getUserType());
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()) {
			resultUser=new User();
			resultUser.setId(rs.getInt("id"));
			resultUser.setUserName(rs.getString("userName"));
			resultUser.setPassWord(rs.getString("password"));
			resultUser.setUserType(rs.getString("userType"));
			
			
		}
		return resultUser;
	}
	
	
	/**
	 * 用户添加
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int add(Connection con,User user)throws Exception{
		String sql="insert into t_user values(null,?,?,?,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1,user.getUserName());
		pstmt.setString(2,user.getPassWord() );
		pstmt.setString(3, user.getSex());
		pstmt.setString(4, user.getUserType());
		pstmt.setString(5, user.getTel());
		pstmt.setString(6 ,user.getEmail());
		return pstmt.executeUpdate();
		
	}
	
	
	/**
	 * 查询用户信息集合
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public ResultSet list(Connection con,User user)throws Exception{
		StringBuffer stb=new StringBuffer("select * from t_user");
		if(StringUtil.isNotEmpty(user.getUserName())) {
			stb.append(" and userName like '%"+user.getUserName()+"%'");
		}
		if(StringUtil.isNotEmpty(user.getUserType())) {
			stb.append(" and userType like '%"+user.getUserType()+"%'");
		}
		if(StringUtil.isNotEmpty(user.getTel())) {
			stb.append(" and tel like '%"+user.getTel()+"%'");
		}
		if(StringUtil.isNotEmpty(user.getPassWord())) {
			stb.append(" and passWord like '%"+user.getPassWord()+"%'");
		}
//		if(user.getUserType()!=null) {
//			stb.append(" and userType="+user.getUserType());
//		}
		PreparedStatement pstmt=con.prepareStatement(stb.toString().replaceFirst("and", "where"));
		return pstmt.executeQuery();
	}
	
	public ResultSet list2(Connection con,User user)throws Exception{
		String sql="select * from t_user where userName=? and passWord=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getPassWord());
		return pstmt.executeQuery();
	}
	
	
	/**
	 * 用户信息删除
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con,String id) throws Exception {
		String sql="delete from t_user where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, id);
		return pstmt.executeUpdate();
	}
	
	
	/**
	 * 用户信息修改
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int update(Connection con,User user) throws Exception{
		String sql="update t_user set userName =?,passWord=?,sex=?,userType=?,tel=?,email=? where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getPassWord());
		pstmt.setString(3, user.getSex());
		pstmt.setString(4, user.getUserType());
		pstmt.setString(5, user.getTel());
		pstmt.setString(6, user.getEmail());
		pstmt.setInt(7, user.getId());		
		return pstmt.executeUpdate();
		
	}
	
}
